package keeto.com.vn.bms.ui.adapter;

import java.util.ArrayList;

import keeto.com.vn.bms.config.IBMSConfig;
import keeto.com.vn.bms.model.Hospital;
import keeto.com.vn.bms.ui.activity.R;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class ListHospitalAdapter extends BaseAdapter {
	private Activity mContext;
	LayoutInflater inflater;
	public static ArrayList<Hospital> listHospital;
	int iClick;
	private View previousView;

	public ListHospitalAdapter(Context context, ArrayList<Hospital> hospitals) {

		// TODO Auto-generated constructor stub
		this.mContext = (Activity) context;
		this.listHospital = hospitals;
		this.inflater = LayoutInflater.from(mContext);

	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		final ViewHolder holder;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.item_lst_stattic_hospital,
					parent, false);
			holder = new ViewHolder();
			holder.tvNameHospital = (TextView) convertView
					.findViewById(R.id.edt_adapter_hospital);
			holder.edtIncome = (EditText) convertView
					.findViewById(R.id.edt_hos_adapter_revenue);
			holder.view = (TableRow) convertView.findViewById(R.id.tableRowHos);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		if (listHospital.get(position).GetSelected() == true) {
			holder.tvNameHospital
					.setBackgroundResource(R.drawable.bg_btn_save_selector);
			holder.edtIncome.setFocusable(true);
			holder.edtIncome.setFocusableInTouchMode(true);
		} else {
			holder.tvNameHospital
					.setBackgroundResource(R.drawable.et_adapter_selector);

		}
		holder.rowBotHos = (View) convertView.findViewById(R.id.rowBot_hos);
		holder.edtIncome.setText(listHospital.get(position).getRevenue());
		holder.tvNameHospital.setText(listHospital.get(position).getName());
//		holder.edtIncome.setEnabled(false);
//		holder.edtIncome.setFocusable(false);
		// holder.edtIncome.setClickable(false);
		if (listHospital.get(position).getRevenue().equals("")
				|| listHospital.get(position).getRevenue().equals("0")) {
			holder.edtIncome.setBackgroundResource(R.drawable.bg_popup_null);
			holder.edtIncome.setHint(mContext.getResources().getString(
					R.string.import_count_revenue));

		} else {
			holder.edtIncome.setText(listHospital.get(position).getRevenue());

		}

		holder.tvNameHospital.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				for (int i = 0; i < listHospital.size(); i++) {
					if (i == position) {
						listHospital.get(i).SetSelected(true);
					} else {
						listHospital.get(i).SetSelected(false);
					}

				}

				holder.edtIncome.setEnabled(true);
				// holder.edtIncome.setClickable(true);
				holder.edtIncome.setFocusable(true);
				holder.edtIncome.setActivated(true);
				holder.edtIncome.setFocusableInTouchMode(true);
				Intent intent = new Intent();
				Bundle bundle = new Bundle();
				bundle.putInt("POSITION", position);
				intent.putExtra("BUNDLE", bundle);
				intent.setAction(IBMSConfig.KEY_ACTION_SHOW_TABLE_MEDICAL_RECORD);
				mContext.sendBroadcast(intent);

			}
		});

		holder.edtIncome.setOnFocusChangeListener(new OnFocusChangeListener() {

			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				// TODO Auto-generated method stub

				holder.edtIncome.setHint("");

			}
		});
		holder.edtIncome.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// TODO Auto-generated method stub

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				listHospital.get(position).setRevenue(s.toString());
				// Log.e("listInfoDoctor.get(position)",
				// listHospital.get(position).getRevenue()+".");
			}
		});

		return convertView;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return listHospital.size();

	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return listHospital.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	// public View setSelected(View view) {
	// if (previousView != null) {
	// previousView.setBackgroundResource(R.drawable.bg_btn_save_selector);
	// }
	// previousView = view;
	// view.setBackgroundResource(R.drawable.et_adapter_selector);
	// return view;
	// }

	class ViewHolder {
		TextView tvNameHospital;
		EditText edtIncome;
		View rowBotHos;
		TableRow view;
	}

}
